import Vue from 'vue'
import Vuex from 'vuex'
// 引入持久化工具
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [
    createPersistedState({
      key: 'HAOKE_ZUFANG', //key指定要持久化的数据的本地存储空间名称
      reducer({
        token,
        curentCity,
        curentCityObj,
        houseCondition,
        queryParams,
        searchResultRommunity,
        rentHouse
      }) {
        //reducer指定需要持久化的且存放在state中的数据
        //第一个参数是state
        return {
          token,
          curentCity,
          curentCityObj,
          houseCondition,
          queryParams,
          searchResultRommunity,
          rentHouse
        } //必须要把持久化的变量return出去
      }
    })
  ],
  state: {
    token: '',
    curentCity: '上海', //当前城市
    curentCityObj: {}, //当前城市对象,需要使用其id
    houseCondition: {}, //房屋查询条件
    queryParams: {
      //合成的查询条件
      cityId: 'AREA|dbf46d32-7e76-1196', //给个当前城市对应初始值，不然新用户从找房登录会报错
      area: null,
      subway: null,
      price: null,
      rentType: null,
      start: 1,
      end: 200,
      more: null
    },
    // 合成房屋发布条件
    rentHouse: {
      title: null,
      description: null,
      houseImg: null,
      oriented: null,
      supporting: null,
      price: null,
      roomType: null,
      size: null,
      floor: null,
      community: null
    }
  },
  getters: {
    isLogin(state) {
      return !!state.token // 两个取反可以把返回的变量转化为返回布尔值
    }
  },
  mutations: {
    // 每次登录都会在login组件中触发这个函数给token重新赋值
    getUserToken(state, token) {
      // console.log(state.token)
      state.token = token
      // console.log(state.token)
    },
    getCurentCityFn(state, curentCity) {
      state.curentCity = curentCity
    },
    getcurentCityObj(state, curentCityObj) {
      state.curentCityObj = curentCityObj
    },
    gethouseCondition(state, houseCondition) {
      state.houseCondition = houseCondition
    },
    getQueryParams(state, queryParams) {
      state.queryParams = queryParams
    },
    updateRentHouse(state, rentHouse) {
      state.rentHouse = rentHouse
    }
  },
  actions: {},
  modules: {}
})
